package com.huaqin.obeacons;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import com.huaqin.obeacons.bmob.Users;
import com.huaqin.obeacons.utils.SharedPreferenceUtil;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends Activity {
	private ImageButton backHomeView;
	private TextView usrNameView;
	private TextView usrNumberView;
	private TextView usrDeptView;
	private TextView usrEmailView;
	private final String TAG = "UserInfoActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_usrinfo);
		backHomeView = (ImageButton) findViewById(R.id.btn_home);
		backHomeView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		usrNameView = (TextView) findViewById(R.id.usr_name);
		usrNumberView = (TextView) findViewById(R.id.usr_number);
		usrDeptView = (TextView) findViewById(R.id.usr_dept);
		usrEmailView = (TextView) findViewById(R.id.usr_email);

		usrNameView.setText(SharedPreferenceUtil.getName(this));
		usrNumberView.setText(SharedPreferenceUtil.getAccount(this));

		BmobQuery<Users> query = new BmobQuery<Users>();
		query.addWhereEqualTo("account", SharedPreferenceUtil.getAccount(this));
		query.findObjects(this, new FindListener<Users>() {

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Log.d(TAG, "onError arg0 " + arg0 + " arg1 " + arg0);
				if (arg1.startsWith("The network is not normal")) {
					Toast.makeText(getApplicationContext(),
							getResources().getString(R.string.network_fail),
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onSuccess(List<Users> list) {
				// TODO Auto-generated method stub
				Log.d(TAG,
						"onSuccess list " + list + " list.size() "
								+ list.size());
				if (list != null && list.size() == 1) {
					usrDeptView.setText(list.get(0).getDept());
					usrEmailView.setText(list.get(0).getEmail());
				}
			}

		});

	}
}
